/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.almac;

import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Almacen;
import dp1.titandevelop.titano.persistent.TipoMovimiento;
import dp1.titandevelop.titano.service.AlmacenService;
import dp1.titandevelop.titano.service.ConexionService;
import dp1.titandevelop.titano.service.TipoMovimientoService;
import dp1.titandevelop.titano.service.ValidacionService;
import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Yulian
 */
public class ModulosT_ALM_ReporteMovimientos extends javax.swing.JInternalFrame {

    private List<Almacen> listaAlm;
    private List<TipoMovimiento> listaTM;
    
    private Mensajes sms = new Mensajes();
    
    private void addList (){
        
        for(int i=0;i<listaAlm.size();i++){
            this.cboxAlmacen.addItem(listaAlm.get(i).getDescripcion());
        }
    }
    private void addListTM (){
        
        for(int i=0;i<listaTM.size();i++){
            this.cboxTipoMovimiento.addItem(listaTM.get(i).getDescripcion());
        }
    }
    public ModulosT_ALM_ReporteMovimientos() {
        initComponents();
        ValidacionService val = new ValidacionService();
        val.NoEscribe(dpFechaFin);
        val.NoEscribe(dpFechaIn);
        
        AlmacenService servAlm = new AlmacenService();
        TipoMovimientoService servTM = new TipoMovimientoService();
        this.listaAlm = servAlm.buscar();
        this.listaTM = servTM.buscar();
        addList();
        addListTM();
    }
    public int cboxIndex(JComboBox cbox){
        return cbox.getSelectedIndex()-1;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnReporte = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cboxAlmacen = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        dpFechaIn = new org.jdesktop.swingx.JXDatePicker();
        jLabel7 = new javax.swing.JLabel();
        dpFechaFin = new org.jdesktop.swingx.JXDatePicker();
        jLabel2 = new javax.swing.JLabel();
        cboxTipoMovimiento = new javax.swing.JComboBox();

        setResizable(true);
        setTitle("Reporte de Movimientos de Material");

        btnReporte.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/reporte2.png"))); // NOI18N
        btnReporte.setToolTipText("Click para generar reporte");
        btnReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReporteActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Almacén:");

        cboxAlmacen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar" }));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Periodo"));

        jLabel6.setText("Fecha Mín:");

        jLabel7.setText("Fecha Máx:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dpFechaIn, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(dpFechaFin, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addGap(4, 4, 4))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(dpFechaIn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(dpFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8))
        );

        jLabel2.setText("Tipo de Movimiento:");

        cboxTipoMovimiento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cboxAlmacen, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(49, 49, 49)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cboxTipoMovimiento, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnReporte)
                        .addGap(0, 0, 0)
                        .addComponent(btnCancelar)))
                .addGap(8, 8, 8))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboxAlmacen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(cboxTipoMovimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnReporte, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(8, 8, 8))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(1, 1, 1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReporteActionPerformed
        
        ValidacionService val = new ValidacionService();
        
        if((!val.esFechaVacia(dpFechaIn)&&!val.esFechaVacia(dpFechaFin))){   //Obligado el periodo         
            
            ConexionService serv = new ConexionService();
            Connection con =serv.getConexion();
            try{


                //Preparando el parámetros
                int indexAlm =cboxIndex(cboxAlmacen), idAlm;
                int indexTM = cboxIndex(cboxTipoMovimiento),idTM;

                Date in = this.dpFechaIn.getDate();
                Date fin = this.dpFechaFin.getDate();
                String cad="";
                Map parametro = new HashMap();

                InputStream archivoReporte;
                if(!val.esCBoxVacio(cboxAlmacen)&& val.esCBoxVacio(cboxTipoMovimiento) ){//Caso en que se elige solo el parametro almacen
                    idAlm = this.listaAlm.get(indexAlm).getIdalmacen();//
                    //cad+= "/reportes/Reporte_Kardex_Fechas_Almacen.jasper";
                    archivoReporte = getClass().getResourceAsStream("/Reportes/Reporte_Kardex_Fechas_Almacen.jasper");
                    parametro.put("almacen",idAlm);
                }
                else if(!val.esCBoxVacio(cboxAlmacen) && !val.esCBoxVacio(cboxTipoMovimiento) ){
                    idAlm = this.listaAlm.get(indexAlm).getIdalmacen();
                    idTM = this.listaTM.get(indexTM).getIdtipomovimiento();
                    
                    //cad+= "\\src\\main\\java\\dp1\\titandevelop\\titano\\view\\Reportes\\Reporte_Kardex_Fechas_Almacen_Tipo.jasper";
                    archivoReporte = getClass().getResourceAsStream("/Reportes/Reporte_Kardex_Fechas_Almacen_Tipo.jasper");
                    parametro.put("almacen",idAlm);
                    parametro.put("tipoMovimiento",idTM);
                }          
                else if(val.esCBoxVacio(cboxAlmacen) && !val.esCBoxVacio(cboxTipoMovimiento) ){
                    idTM = this.listaTM.get(indexTM).getIdtipomovimiento();                    
                    //cad+= "\\src\\main\\java\\dp1\\titandevelop\\titano\\view\\Reportes\\Reporte_Kardex_Fechas_Tipo.jasper";
                    archivoReporte =getClass().getResourceAsStream("/Reportes/Reporte_Kardex_Fechas_Tipo.jasper");
                    parametro.put("tipoMovimiento",idTM);
                }
                else {
                    //cad+= "\\src\\main\\java\\dp1\\titandevelop\\titano\\view\\Reportes\\Reporte_Kardex_Fechas.jasper";
                    archivoReporte = getClass().getResourceAsStream("/Reportes/Reporte_Kardex_Fechas.jasper");                           
                }
                parametro.put("fechaMin",in);
                parametro.put("fechaMax",fin);
                //String archivoReporte = System.getProperty("user.dir")+cad;
                
                JasperReport jreport = (JasperReport) JRLoader.loadObject(archivoReporte);
                JasperPrint jasperPrint = JasperFillManager.fillReport(jreport,parametro,con);
                JasperViewer jviewer = new JasperViewer(jasperPrint,false);  
                jviewer.setVisible(true); 
                
            }catch(Exception e){    
                e.printStackTrace();
                JOptionPane.showInternalMessageDialog(this.getRootPane(),"Hubo un error en la generación del reporte","Error",JOptionPane.ERROR_MESSAGE);

            }
        }
        else{
            JOptionPane.showInternalMessageDialog(this.getRootPane(), "Debe ingresar el período ","Aviso",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnReporteActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnReporte;
    private javax.swing.JComboBox cboxAlmacen;
    private javax.swing.JComboBox cboxTipoMovimiento;
    private org.jdesktop.swingx.JXDatePicker dpFechaFin;
    private org.jdesktop.swingx.JXDatePicker dpFechaIn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
